package com.liruo.treasureattic.starter.httpmessagehandler.handler;

import com.liruo.treasureattic.common.httpmsg.result.BaseResponse;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * @Author:liruo
 * @Date:2022-10-28-09:56:39
 * @Desc
 */
@Deprecated
public class ResultResponseBodyAdvice implements ResponseBodyAdvice<Object> {
    /**
     *
     * @param returnType void 就是 void.class
     * @return
     */
    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        return !returnType.getParameterType().equals(BaseResponse.class) && converterType.equals(MappingJackson2HttpMessageConverter.class);
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        if(body instanceof BaseResponse<?>){
            return body;
        }
        return BaseResponse.success(body);
    }
}
